﻿using AutoMapper;
using HongYeSystemWebApi.Templet.Entity.Entities;
using HongYeSystemWebApi.Templet.Entity.Request;
using HongYeSystemWebApi.Templet.Entity.Request.SysCostType;
using HongYeSystemWebApi.Templet.Entity.Request.SysProject;
using HongYeSystemWebApi.Templet.Entity.Response;
using HongYeSystemWebApi.Templet.Entity.Response.Project;
using HongYeSystemWebApi.Templet.Entity.Response.SysCompanyEntity;
using HongYeSystemWebApi.Templet.Entity.Response.SysCostType;
using HongYeSystemWebApi.Templet.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HongYeSystemWebApi.Templet.Service
{
    public class CostTypeService : ICostTypeService
    {
        private readonly SqlSugarClient _db;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="db"></param>
        public CostTypeService(IHelpDB db, IMapper mapper)
        {
            this._db = db.OpenDB();
            _mapper = mapper;
        }
        public async Task DeleteAsync(MultiIdRequest request)
        {
            List<CostType> DelData = new List<CostType>();
            foreach (var item in request.Ids)
            {
                var project = await _db.Queryable<CostType>().FirstAsync(a => a.Id == item);
                project.Delete = true;
                project.OperatDate = DateTime.Now;
                DelData.Add(project);
            }
           await _db.Updateable(DelData).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }
 

        public async Task InsertAsync(SysCostTypeInsertRequest request)
        {
            var data = _mapper.Map<CostType>(request);
            data.OperatDate = DateTime.Now;
            var dataFir = await _db.Queryable<CostType>().FirstAsync(a => !a.Delete && a.CostName == data.CostName);
            if (dataFir != null)
            {
                throw new Exception("存在相同名称的数据，请修改名称后再次添加");
            }
            await _db.Insertable(data).ExecuteCommandAsync();
        }
      
        public async Task UpdateAsync(SysCostTypeUpdataRequest request)
        {
            var datafir = await _db.Queryable<CostType>().FirstAsync(X => X.Id == request.Id);
            if (datafir == null)
            {
                throw new Exception("未找到修改原始数据");
            }
            var data = _mapper.Map<CostType>(request);
            data.OperatDate = DateTime.Now;
            await _db.Updateable(data).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }

        public async Task<PageModel<SysCostTypeDataPageResponse>> GetListAsync(KeyPageRequest request)
        {
            var data = _db.Queryable<CostType>().Where(a => !a.Delete);
            if (!string.IsNullOrEmpty(request.key))
            {
                data = data.Where(a=> (a.CostName.Contains(request.key)));
            }
            RefAsync<int> Total = 0;
            //var resultpage = await data.ToPageListAsync(request.PageSize, request.PageNumber, Total);
            var resultpage = await data.OrderBy(x => x.Id, OrderByType.Desc).ToPageListAsync(request.PageNumber, request.PageSize, Total);
            var result = _mapper.Map<List<SysCostTypeDataPageResponse>>(resultpage);
            return new PageModel<SysCostTypeDataPageResponse>() { Data = result, Total = Total };
        }

        public async Task GetNameAsync(KeyPageRequest request)
        {

            var dataFir = await _db.Queryable<CostType>().FirstAsync(a => !a.Delete && a.CostName == request.key);
            if (dataFir != null)
            {
                throw new Exception("存在相同名称的数据，请修改名称后再次添加");
            }
        }
    }
}
